//
//  UIButton+extention.swift
//  01weibo
//
//  Created by 李想 on 16/6/4.
//  Copyright © 2016年 李想. All rights reserved.
//

import UIKit

extension UIButton {
    //Initializer 'init()' with Objective-C selector 'init' conflicts with implicit initializer 'init()' with the same Objective-C selector: 自定义的init方法和NSObject的init方法冲突
    //在定义swift的函数时，如果函数的参数有可能在某个时间被设为nil，或者有可能被传一个nil，则将该参数设为可选。
    convenience init (title: String?,
        titleColor: UIColor = UIColor.blackColor(),
        titleFont:UIFont = UIFont.systemFontOfSize(12),
        imageName: String?,
        backImageName: String?,
        target:AnyObject? = nil,
        action:String?,
        event:UIControlEvents = UIControlEvents.TouchUpInside
        )
        
        {
            self.init()
            
            self.setTitle(title, forState: .Normal)
            self.setTitleColor(titleColor, forState: .Normal)
            
            self.titleLabel!.font = titleFont
            if imageName != nil{
                self.setImage(UIImage(named: imageName!) , forState:.Normal)
                self.setImage(UIImage(named: "\(imageName!)_highlighted") , forState:.Highlighted)
            }
                
            if backImageName != nil{
                self.setBackgroundImage(UIImage(named: backImageName!), forState: .Normal)
                self.setBackgroundImage(UIImage(named:"\(backImageName)_highlighted" ), forState: .Highlighted)
            }
             //action如果直接写，可以直接用字符串，如果作为参数传递，必须使用 selector
            let sel = Selector(action!)
            self.addTarget(target, action: sel, forControlEvents: event)
                
        }
 
}
